//对象的定义
var xiaoming={
    name :'小明',
    birth:2001
}
//给对象绑定一个函数，比如写一个方法，返回年龄
var xiaoming={
    name :'小明',
    birth:2001,
    age:function(){
        var y=new Date().getFullYear();
        return y-this.birth;
    }
};
console.log(xiaoming.age);//
console.log(xiaoming.age());;//20
//------------
function getage(){
    var y=new Date().getFullYear();
    return y -this.birth;
}
var xiaomin={
    name:'小明',
    birth:2001,
    age:getage
};
console.log(xiaomin.age());;//20
getage();//nan
console.log(getage());//nan
//-----------------
var xiaoho={
    name:'小红',
    birth:1990,
    age:function(){
        var y=new Date().getFullYear();
        return y-this.birth;
    }
};
var fun=xiaoho.age;
console.log(fun());;//指出一个错误
//---------------------重构方法
var xiaoliang={
    name:'小两',
    birth:2001,
    age:function(){
        function getagefrombirth(){
            var y=new Date().getFullYear();
            return y-this.birth;
        }
    }
};
//xiaoliang.age();//爆出错误，在age方法里指向xiaoliang ,在内部定义函数指向undefined
//--------修复方法
var xiaojun={
    name:'小军',
    birth:2005,
    age:function(){
        var that=this;
        function aaa(){
            var y=new Date().getFullYear();
            return y-that.birth;
        }
    }
}
console.log(xiaojun.age());;///一开始就铺获this
//--------------
function getaage(){
    var y=new Date().getFullYear();
    return y-this.birth;
}
var xiaolei={
    name:'小雷',
    birth:2001,
    age:getaage
};
console.log(xiaolei.age());;//20
console.log(getaage.apply(xiaolei));;//this指向xiaolei//20
//-------------
Math.max.apply(null,[7,3,4,5]);
//对普通函数调用，我们通常把this绑定为null。
Math.max.call(null,3,5,4)
//唯一区别是：

//apply()把参数打包成Array再传入；

//call()把参数按顺序传入。
var count=0;
var oldparseint=parseInt
window.parseInt=function(){
    count+=1;
    return oldparseint.apply(null,arguments);
};
console.log(parseInt('10'));
parseInt('20');
parseInt('30');
console.log('count = ' + count);

//-----------------
